#include "DeadLetterQueue.h"
#include "utils/utils.h"

namespace zeroplus::mq
{
    DeadLetterQueue::DeadLetterQueue(std::shared_ptr<sw::redis::Redis> redisFd)
        : redis_(redisFd),
          topic_("DLQ")
    {
        CreateStreams();
    }

    DeadLetterQueue::~DeadLetterQueue()
    {
    }

    std::string DeadLetterQueue::CreateStreams()
    {
        if (!IsTopicExists({topic_}))
        {
            std::vector<std::pair<std::string, std::string>> attrs = {{"message", "DeadLetterQueue"}};
            redis_->xadd(topic_, "*", attrs.begin(), attrs.end());
        }
        return "";
    }

    std::string DeadLetterQueue::WriteToStorage(const std::string &messageId, const Bytes &message)
    {
        std::vector<std::pair<std::string, std::string>> attrs = {{"message", message.to_string()}};
        return redis_->xadd(topic_, messageId, attrs.begin(), attrs.end());
    }

    long long DeadLetterQueue::IsTopicExists(const std::vector<std::string> &topicList)
    {
        auto res = redis_->exists(topicList.begin(), topicList.end());
        return res;
    }

}
